@extends('Admin.base')
@section('css')

@endsection
@section('style')

@endsection
@section('content')
    <div class="layui-card">
        <div class="layui-form layui-card-header layuiadmin-card-header-auto">
            <div class="layui-form-item layui-row">

                <div class="layui-inline layui-col-xs2 layui-col-sm2 layui-col-md2">
                    <label class="layui-form-label">状态</label>
                    <div class="layui-input-block">
                        <select name="status" >
                            <option value=''>全部状态</option>
                            @foreach (\App\Models\ActivitySale::ACTIVITY_STATUS as $id => $name)
                                <option value="{{$id}}">{{$name}}</option>
                            @endforeach
                        </select>
                    </div>
                </div>

                <div class="layui-inline">
                    <div class="layui-btn-group layui-input-block">
                        <button class="layui-btn layui-btn-sm layui-input-block layuiadmin-btn-admin" lay-submit lay-filter="LAY-search">
                            <i class="layui-icon layui-icon-search layuiadmin-button-btn"></i>
                        </button>
                    </div>
                </div>
            </div>
        </div>
        <div class="layui-card-body">
            <table id="LAY-table" lay-filter="LAY-table"></table>

            <script type="text/html" id="tableToolbar">

                <a href="/admin/offer/edit/@{{ d.id }}" class="layui-btn layui-btn-green layui-btn-xs">
                    编辑
                </a>

                @{{# if(d.status == 1 ){ }}
                <a class="layui-btn layui-btn-xs layui-btn-danger"  lay-event="updataStatus">
                    停用
                </a>
                @{{# }else  { }}
                <a class="layui-btn layui-btn-xs layui-btn-normal" lay-event="updataStatus">
                    启动
                </a>
                @{{# } }}
            </script>

            <script type="text/html" id="toolbarDemo">
                <div class="layui-btn-container">
                    <a class="layui-btn" href="{{ route('offer.add') }}">新 建</a>
                </div>
            </script>
            <input type="hidden" value="" id="num">
        </div>
    </div>
@endsection

@section('script')
    <script src="/js/ajax.js"></script>
    <script>
        layui.use(['form', 'table', 'laydate'], function(){
            var $ = layui.$
                ,form = layui.form
                ,table = layui.table

            table.render({
                elem: '#LAY-table'
                ,toolbar: '#toolbarDemo'
                ,defaultToolbar: []
                ,url: "{{url('/admin/offer/table')}}"
                ,method: "post"
                ,page: true //开启分页
                ,limit:10
                ,cols: [[
                    {field:'activity_name', title:'活动名称' }
                    ,{field:'less', title:'活动规则' ,templet:function(d){
                        return  "满" + d.full + "减" + d.less;
                    }}
                    ,{field: 'status', title: '活动状态' ,templet:function(d){
                        return d.status == 1 ? "启用" : "停用";
                    }}
                    ,{field: 'scope', title: '有效范围' ,templet:function(d){
                        return d.scope == 1 ? "全部商品" : "部分商品";
                    }}
                    ,{field: 'start_at', title: '活动开始日期',templet:function(d){
                            return d.start_at?d.start_at.substring(0,10):''  ;
                    }}
                    ,{field: 'end_at', title: '活动结束日期',templet:function(d){
                        return d.end_at?d.end_at.substring(0,10):''  ;
                    }}
                    ,{field: 'amount_use', title: '付款订单数' ,templet:function(d){
                        return d.amount_use   ? d.amount_use  : 0;
                    }}
                    ,{fixed: 'right', title: '操作' , width: 200, align:'center', toolbar: '#tableToolbar'}
                ]]

            });

            //监听工具条
            table.on('tool(LAY-table)', function(obj){
                var data = obj.data;
                console.log(obj.event)
                 if(obj.event === 'updataStatus'){
                    layer.confirm('确认修改状态吗?', function(index){
                        $.ajax({
                            url: '/admin/offer/upstatus/'+ data.id
                            , type: 'POST'
                            , dataType: 'json'
                            , data: {'status': data.status }
                            , success: function (ret) {
                                if (ret.hasOwnProperty('code') && ret.code == 200) {
                                    layer.msg(ret.message,{icon: 1,time:2000});
                                    table.reload('LAY-table');
                                    return false;
                                }
                                layer.msg(ret.message,{icon:2});
                            }
                        });
                    });
                }
            });
            //监听搜索
            form.on('submit(LAY-search)', function(data){
                var field = data.field;
                //执行重载
                table.reload('LAY-table', {
                    where: field,page: {curr: 1}
                });
            });
        });
    </script>

@endsection